<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *	has readed
 *      $Id: space_index.php 31354 2012-08-16 03:03:08Z chenmengshu $
 */
//个人空间首页
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
//全局-广播设置 默认查看个人资料
if(($_G['adminid'] == 1 && $_G['setting']['allowquickviewprofile'] && $_GET['view'] != 'admin' && $_GET['diy'] != 'yes') || defined('IN_MOBILE')) {
	dheader("Location:home.php?mod=space&uid=$space[uid]&do=profile");
}

require_once libfile('function/space');

space_merge($space, 'field_home');//用户家园字段表
/*
 * $defaultdiy = array(
			'currentlayout' => '1:2:1',
			'block' => array(
					'frame`frame1' => array(
							'attr' => array('name'=>'frame1'),
							'column`frame1_left' => array(
									'block`profile' => array('attr' => array('name'=>'profile')),//头像
									'block`statistic' => array('attr' => array('name'=>'statistic')),//统计信息
									'block`album' => array('attr' => array('name'=>'album')),//相册
									'block`doing' => array('attr' => array('name'=>'doing'))//记录
							),
							'column`frame1_center' => array(
									'block`personalinfo' => array('attr' => array('name'=>'personalinfo')),//个人资料
									'block`feed' => array('attr' => array('name'=>'feed')),//动态
									'block`share' => array('attr' => array('name'=>'share')),//分享
									'block`blog' => array('attr' => array('name'=>'blog')),//日志
									'block`thread' => array('attr' => array('name'=>'thread')),//主题
									'block`wall' => array('attr' => array('name'=>'wall'))//留言板
							),
							'column`frame1_right' => array(
									'block`myapp' => array('attr' => array('name'=>'myapp')),//功能废弃
									'block`friend' => array('attr' => array('name'=>'friend')),//好友
									'block`visitor' => array('attr' => array('name'=>'visitor')),//最近 访问
									'block`group' => array('attr' => array('name'=>'group'))//群组
							)
					)
			),
			'parameters' => array(
					'blog' => array('showmessage' => 150, 'shownum' => 6),
					'doing' => array('shownum' => 15),
					'album' => array('shownum' => 8),
					'thread' => array('shownum' => 10),
					'share' => array('shownum' => 10),
					'friend' => array('shownum' => 18),
					'group' => array('shownum' => 12),
					'visitor' => array('shownum' => 18),
					'wall' => array('shownum' => 16),
					'feed' => array('shownum' => 16),
					'myapp' => array('shownum' => 9, 'logotype'=> 'logo'),
			),
		'nv' => array(
			'nvhidden' => 0,
			'items' => array(),
			'banitems' => array(),
		),
	);
 */
$userdiy = getuserdiydata($space);

if ($_GET['op'] == 'getmusiclist') {//ajax 获取音乐盒歌曲列表
	if(empty($space['uid'])) {
		exit();
	}
	$reauthcode = substr(md5($_G['authkey'].$space['uid']), 6, 16);
	if($reauthcode == $_GET['hash']) {
		// 后端直接返回, 由前端负责将原始数据调整为所需格式
		helper_output::json($userdiy['parameters']['music']);
	}
	exit();

}
else{
	//全局-性能优化-服务器优化 查看数开启防刷新,选择“否”，主题、空间、日志每访问一次，浏览量增1
	//或观看别人的空间,第一次看它的空间,第二次看不增,看了别人,再看回它才增
	if(!$_G['setting']['preventrefresh'] || $_G['uid'] && !$space['self'] && $_G['cookie']['viewid'] != 'uid_'.$space['uid']) {
		member_count_update($space['uid'], array('views' => 1));
		$viewuids[$space['uid']] = $space['uid'];
		dsetcookie('viewid', 'uid_'.$space['uid']);
	}

	if(!$space['self'] && $_G['uid'] && $_GET['additional'] != 'removevlog') {//访问别人空间
		$visitor = C::t('home_visitor')->fetch_by_uid_vuid($space['uid'], $_G['uid']);//我对对方的访问记录
		$is_anonymous = empty($_G['cookie']['anonymous_visit_'.$_G['uid'].'_'.$space['uid']]) ? 0 : 1;
		if(empty($visitor['dateline'])) {
			$setarr = array(
				'uid' => $space['uid'],
				'vuid' => $_G['uid'],
				'vusername' => $is_anonymous ? '' : $_G['username'],
				'dateline' => $_G['timestamp']
			);
			C::t('home_visitor')->insert($setarr, false, true);//访问增加
			show_credit();
		} else {
			if($_G['timestamp'] - $visitor['dateline'] >= 300) {//超过5分钟更新访问时间
				C::t('home_visitor')->update_by_uid_vuid($space['uid'], $_G['uid'], array('dateline'=>$_G['timestamp'], 'vusername'=>$is_anonymous ? '' : $_G['username']));
			}
			if($_G['timestamp'] - $visitor['dateline'] >= 3600) {
				show_credit();
			}
		}
		updatecreditbyaction('visit', 0, array(), $space['uid']);//0代表我,我访问别人空间加积分 $space['uid']用于去重,意味着重复访问它的空间不会重复加积分
	}
	if($_GET['additional'] == 'removevlog') {//删除访问足迹 ,js调用
		//如我访问李四空间$space['uid'],有记录  删除我$_G['uid']对李四的记录
		C::t('home_visitor')->delete_by_uid_vuid($space['uid'], $_G['uid']);
	}

	if($do != 'profile' && !ckprivacy($do, 'view')) {//此处代码多余,这个模块是空间首页,能进来说明do=index,ckprivacy对模块是否隐私的处理在home_space.php已经处理
		$_G['privacy'] = 1;
		require_once libfile('space/profile', 'include');
		include template('home/space_privacy');
		exit();
	}

	$widths = getlayout($userdiy['currentlayout']);
	$leftlist = formatdata($userdiy, 'left', $space);
	$centerlist = formatdata($userdiy, 'center', $space);
	$rightlist = formatdata($userdiy, 'right', $space);

	dsetcookie('home_diymode', 1);//空间首页可以diy 显示那些模块
}
//spacename 空间名称
$navtitle = !empty($space['spacename']) ? $space['spacename'] : lang('space', 'sb_space', array('who' => $space['username']));
$metakeywords = lang('space', 'sb_space', array('who' => $space['username']));//'{who}的空间',
$metadescription = lang('space', 'sb_space', array('who' => $space['username']));
$space['medals'] = getuserprofile('medals');
if($space['medals']) {//mark 需要优化 不知道获取勋章干什么,头像模块的勋章在getblockhtml获取了
	loadcache('medals');
	foreach($space['medals'] = explode("\t", $space['medals']) as $key => $medalid) {
		list($medalid, $medalexpiration) = explode("|", $medalid);
		if(isset($_G['cache']['medals'][$medalid]) && (!$medalexpiration || $medalexpiration > TIMESTAMP)) {
			$space['medals'][$key] = $_G['cache']['medals'][$medalid];
		} else {
			unset($space['medals'][$key]);
		}
	}
}
include_once(template('home/space_index'));

function formatdata($data, $position, $space) {
	$list = array();//'block`profile' => array('attr' => array('name'=>'profile')),
	foreach ((array)$data['block']['frame`frame1']['column`frame1_'.$position] as $blockname => $blockdata) {
		if (strpos($blockname, 'block`') === false || empty($blockdata) || !isset($blockdata['attr']['name'])) continue;
		$name = $blockdata['attr']['name'];//模块名称如相册,个人资料
		if(check_ban_block($name, $space)) {//对应模块是否可以展示
			$list[$name] = getblockhtml($name, $data['parameters'][$name]);
		}
	}
	return $list;
}

function show_credit() {
	global $_G, $space;

	$showinfo = C::t('home_show')->fetch($space['uid']);//用户上榜表
	if($showinfo['credit'] > 0) {//花了  上榜总积分
		$showinfo['unitprice'] = intval($showinfo['unitprice']);//单次访问单价
		if($showinfo['credit'] <= $showinfo['unitprice']) {//上榜总积分<单次访问单价,不够钱了,不给你推广了
			notification_add($space['uid'], 'show', 'show_out');//访问了您的主页后，您在竞价排名榜中的最后一个积分也被消费掉了
			C::t('home_show')->delete($space['uid']);
		} else {
			C::t('home_show')->update_credit_by_uid($space['uid'], -$showinfo['unitprice']);
		}
	}
}
?>